/*
 * Copyright(C) 2012 Luvina Software Company
 *
 * MstGroupDaoImpl.java, May 29, 2012 HaiPD
 */
package net.luvina.manageuser.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import net.luvina.manageuser.dao.MstGroupDao;
import net.luvina.manageuser.hibernate.MstGroup;

/**
 * MstGroupDaoImpl implements MsGroupDao
 *
 * @author HaiPD
 *
 */
@Repository("groupDao")
public class MstGroupDaoImpl extends GenericDaoImpl<MstGroup, Integer>
		implements MstGroupDao {

	public MstGroupDaoImpl() {
		super(MstGroup.class);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see net.luvina.manageuser.dao.MstGroupDao#getGroupList()
	 */
	@SuppressWarnings("unchecked")
	public List<MstGroup> getListGroup() {
		HibernateCallback callback = new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuilder sqlCommand = new StringBuilder();
				sqlCommand.append("SELECT ");
				sqlCommand.append("group_id as groupId, ");
				sqlCommand.append("group_name as groupName ");
				sqlCommand.append("FROM mst_group as g ");

				Query query = session.createSQLQuery(sqlCommand.toString())
						.addScalar("groupId", Hibernate.INTEGER).addScalar(
								"groupName", Hibernate.STRING)
						.setResultTransformer(
								Transformers.aliasToBean(MstGroup.class));
				return (List<MstGroup>) query.list();
			}

		};
		return (List<MstGroup>) getHibernateTemplate().execute(callback);
	}

}
